import { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { getQuestionList } from "../service";
import { SEARCH_PARAMS_KEY, SEARCH_PARAMS_PAGESIZE, SEARCH_PARAMS_PAGE_DEFAULT, SEARCH_PARAMS_PAGE } from "../constant/index";
import { useRequest } from "ahooks";
type Optiontypes = {
  isStart: boolean;
  isDelete: boolean;
  page: number;
  pageSize: number;
};

function useGetQuestionByKey(opt: Partial<Optiontypes>) {
  const { isStart, isDelete } = opt;
  const [params] = useSearchParams();
  function getquestionlist() {
    const keyword = params.get(SEARCH_PARAMS_KEY) || "";
    const pageSize = parseInt(params.get(SEARCH_PARAMS_PAGESIZE) || "") || SEARCH_PARAMS_PAGE_DEFAULT;
    const page = parseInt(params.get(SEARCH_PARAMS_PAGE) || "") || 1;
    return getQuestionList({ keyword, isStart, isDelete, pageSize, page });
  }
  const { data, error, loading, refresh } = useRequest(getquestionlist, {
    refreshDeps: [params],
  });
  // console.log(loading, data);
  return { loading, data, refresh };
}

export default useGetQuestionByKey;
